﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Util.Date
{
    
    /// <summary>
    /// DateTimePicker.xaml 的交互逻辑
    /// </summary>    
    public partial class DateTimePicker : UserControl
    {

        public DateTimePicker()
        {
            InitializeComponent();
            this._minute.Background = System.Windows.Media.Brushes.White;
           // this._second.Background = System.Windows.Media.Brushes.White;
            this._hour.Background = System.Windows.Media.Brushes.White;
        }
        /// <summary>
        /// 更改选中状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textbox_hour_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                switch (tb.Name)
                {
                    case "_hour":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        this._minute.Background = System.Windows.Media.Brushes.White;
                        //this._second.Background = System.Windows.Media.Brushes.White;
                        this._year.Background = System.Windows.Media.Brushes.White;
                        this._month.Background = System.Windows.Media.Brushes.White;
                        this._day.Background = System.Windows.Media.Brushes.White;
                        break;
                    case "_minute":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        this._hour.Background = System.Windows.Media.Brushes.White;
                        //this._second.Background = System.Windows.Media.Brushes.White;
                        this._year.Background = System.Windows.Media.Brushes.White;
                        this._month.Background = System.Windows.Media.Brushes.White;
                        this._day.Background = System.Windows.Media.Brushes.White;
                        break;
                    //case "_second":
                    //    tb.Background = System.Windows.Media.Brushes.Gray;
                    //    this._hour.Background = System.Windows.Media.Brushes.White;
                    //    this._minute.Background = System.Windows.Media.Brushes.White;
                    //    this._year.Background = System.Windows.Media.Brushes.White;
                    //    this._month.Background = System.Windows.Media.Brushes.White;
                    //    this._day.Background = System.Windows.Media.Brushes.White;
                    //    break;
                    case "_year":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        this._hour.Background = System.Windows.Media.Brushes.White;
                        this._minute.Background = System.Windows.Media.Brushes.White;
                       // this._second.Background = System.Windows.Media.Brushes.White;
                        this._month.Background = System.Windows.Media.Brushes.White;
                        this._day.Background = System.Windows.Media.Brushes.White;
                        break;
                    case "_month":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        this._hour.Background = System.Windows.Media.Brushes.White;
                        this._minute.Background = System.Windows.Media.Brushes.White;
                        //this._second.Background = System.Windows.Media.Brushes.White;
                        this._year.Background = System.Windows.Media.Brushes.White;
                        this._day.Background = System.Windows.Media.Brushes.White;
                        break;
                    case "_day":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        this._hour.Background = System.Windows.Media.Brushes.White;
                        this._minute.Background = System.Windows.Media.Brushes.White;
                        //this._second.Background = System.Windows.Media.Brushes.White;
                        this._year.Background = System.Windows.Media.Brushes.White;
                        this._month.Background = System.Windows.Media.Brushes.White;
                        break;
                }
            }
        }
        private string Connect_Text()
        {
            return _year.Text + "/" + _month.Text + "/" + _day.Text + " "
                        + _hour.Text + ":" + _minute.Text;

        }

        private void Loaded_Text(DateTime dateTime)
        {
            switch (Classification)
            {
                case 0:
                    //textBlock1.Text = dateTime.ToString("yyyy/MM/dd HH:mm:ss");//"yyyyMMddHHmmss"
                    _year.Text = dateTime.Year.ToString();
                    _month.Text = dateTime.Month.ToString();
                    _day.Text = dateTime.Day.ToString();
                    //_second.Text = dateTime.Second.ToString();
                    _minute.Text = dateTime.Minute.ToString();
                    _hour.Text = dateTime.Hour.ToString();
                    string Connect = _year.Text + "/" + _month.Text + "/" + _day.Text + " "
                        + _hour.Text + ":" + _minute.Text ;
                    DateTime = Convert.ToDateTime(Connect);
                    break;

                    //case 1:
                    //    textBlock1.Text = dateTime.ToString("yyyy/MM/dd");//"yyyyMMddHHmmss"
                    //    DateTime = Convert.ToDateTime(textBlock1.Text);
                    //    break;
                    //case 2:
                    //    textBlock1.Text = dateTime.ToString("HH:mm:ss");//"yyyyMMddHHmmss"
                    //    int year = DateTime.Year;
                    //    int month = DateTime.Month;
                    //    int day = DateTime.Day;
                    //    DateTime = Convert.ToDateTime(year.ToString() + "/" + month.ToString() + "/"
                    //        + day.ToString() + " " + textBlock1.Text);
                    //    break;

            }
        }

        public void Up_Click(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            int temp;
            if (this._hour.Background == System.Windows.Media.Brushes.Gray)
            {
                string hour = _hour.Text.ToString();
                temp = System.Int32.Parse(hour);
                temp++;
                if (temp > 23)
                {
                    temp = 0;
                }
                _hour.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            else if (this._minute.Background == System.Windows.Media.Brushes.Gray)
            {
                string minute = _minute.Text.ToString();
                temp = System.Int32.Parse(minute);
                temp++;
                if (temp > 59)
                {
                    temp = 0;
                }
                _minute.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            else 
            //if (this._second.Background == System.Windows.Media.Brushes.Gray)
            //{
            //    string second = _second.Text.ToString();
            //    temp = System.Int32.Parse(second);
            //    temp++;
            //    if (temp > 59)
            //    {
            //        temp = 0;
            //    }
            //    _second.Text = temp.ToString();
            //    DateTime = Convert.ToDateTime(Connect_Text2DateTime());
            //}
            if (this._year.Background == System.Windows.Media.Brushes.Gray)
            {
                string year = _year.Text.ToString();
                temp = System.Int32.Parse(year);
                temp++;
                if (temp > 2019)
                {
                    temp = 2006;
                }
                _year.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            if (this._month.Background == System.Windows.Media.Brushes.Gray)
            {
                string month = _month.Text.ToString();
                temp = System.Int32.Parse(month);
                temp++; 
                if (temp > 12)
                {
                    temp = 1;
                }
                _month.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            if (this._day.Background == System.Windows.Media.Brushes.Gray)
            {
                string day = _day.Text.ToString();
                temp = System.Int32.Parse(day);
                temp++;
                if (temp > 31)
                {
                    temp = 1;
                }
                _day.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
        }



        /// <summary>
        /// 向下减时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Down_Click(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            int temp;
            if (this._hour.Background == System.Windows.Media.Brushes.Gray)
            {
                string hour = _hour.Text.ToString();
                temp = System.Int32.Parse(hour);
                temp--;
                if (temp <0)
                {
                    temp = 23;
                }
                _hour.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            else if (this._minute.Background == System.Windows.Media.Brushes.Gray)
            {
                string minute = _minute.Text.ToString();
                temp = System.Int32.Parse(minute);
                temp--;
                if (temp < 0)
                {
                    temp = 59;
                }
                _minute.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            else 
            //if (this._second.Background == System.Windows.Media.Brushes.Gray)
            //{
            //    string second = _second.Text.ToString();
            //    temp = System.Int32.Parse(second);
            //    temp++;
            //    if (temp<0)
            //    {
            //        temp = 59;
            //    }
            //    _second.Text = temp.ToString();
            //    DateTime = Convert.ToDateTime(Connect_Text2DateTime());
            //}
            if (this._year.Background == System.Windows.Media.Brushes.Gray)
            {
                string year = _year.Text.ToString();
                temp = System.Int32.Parse(year);
                temp--;
                if (temp <2006)
                {
                    temp = 2019;
                }
                _year.Text = temp.ToString();
                DateTime = Convert.ToDateTime( Connect_Text());
            }
            if (this._month.Background == System.Windows.Media.Brushes.Gray)
            {
                string month = _month.Text.ToString();
                temp = System.Int32.Parse(month);
                temp--;
                if (temp < 0)
                {
                    temp = 12;
                }
                _month.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
            if (this._day.Background == System.Windows.Media.Brushes.Gray)
            {
                string day = _day.Text.ToString();
                temp = System.Int32.Parse(day);
                temp--;
                if (temp <0)
                {
                    temp = 31;
                }
                _day.Text = temp.ToString();
                DateTime = Convert.ToDateTime(Connect_Text());
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="txt"></param>
        public DateTimePicker(string txt) : this()
        {
            // this.textBox1.Text = txt;
        }



        public int Classification
        {
            get { return (int)GetValue(ClassificationProperty); }
            set { SetValue(ClassificationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Classification.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClassificationProperty =
            DependencyProperty.Register("Classification", typeof(int), typeof(DateTimePicker), new PropertyMetadata(0));



        public DateTime SelDateTime
        {
            get { return (DateTime)GetValue(SelDateTimeProperty); }
            set { SetValue(SelDateTimeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelDateTime.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelDateTimeProperty =
            DependencyProperty.Register("SelDateTime", typeof(DateTime), typeof(DateTimePicker), new PropertyMetadata(DateTime.Now));


        #region 事件

        /// <summary>
        /// 日历图标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iconButton1_Click(object sender, RoutedEventArgs e)
        {

            if (popChioce.IsOpen == true)
            {
                popChioce.IsOpen = false;
            }

            TDateTimeView dtView = new TDateTimeView(_hour.Text.ToString() + "|" + _minute.Text.ToString() + "|" + Connect_Text());// TDateTimeView  构造函数传入日期时间
            dtView.DateTimeOK += (dateTimeStr) => //TDateTimeView 日期时间确定事件
            {
                DateTime t = Convert.ToDateTime(dateTimeStr);
                Loaded_Text(t);
                //textBlock1.Text = dateTimeStr;
                //DateTime = Convert.ToDateTime(dateTimeStr);
                popChioce.IsOpen = false;//TDateTimeView 所在pop  关闭
            };
            popChioce.Child = dtView;
            popChioce.IsOpen = true;
        }

        /// <summary>
        /// DateTimePicker 窗体登录事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //DateTime dt = DateTime.Now;
            Loaded_Text(DateTime);
        }


        #endregion

        #region 属性

        /// <summary>
        /// 日期时间
        /// </summary>
        public DateTime DateTime { get { return SelDateTime; } set { SelDateTime = value; } }

        #endregion
    }
}
